package chatManagers;

//package wmmd.chat;
//package chatManagers;

/*
 * commanager.java
 *
 * Created on July 20, 2006, 10:03 AM
 */

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.*;

/**
 *
 * @author spring
 * @version
 */
public class viewTranscript extends HttpServlet {
    Hashtable h = new Hashtable();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        //DBMSManager dbm= new DBMSManager(request.getParameter("db"));
        DBMSManager dbm = new DBMSManager();
        
        // Items we'll need to build up the response
        String responseString = new String();
        int cid = Integer.parseInt(request.getParameter("cid"));
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MMM d, yyyy");
        java.text.SimpleDateFormat tf = new java.text.SimpleDateFormat("hh:mm aa");
        
        // Create a table of all users
        ResultSet rs = dbm.getMembers(0);
        try {
            while(rs.next()) {
                h.put(rs.getString("ID"), rs.getString("username"));
            }
            rs.close();
        
            // Write out the header
            out.println("<h2>Transcript for " + dbm.getTitle(cid) + "</h2>");
            out.println("<p>&nbsp;</p>");
            out.println("<p><i>(Chat started on " + df.format(dbm.getStartDate(cid)) + ")</i></p>");
        
            // Write out the transcript of the cat
            ResultSet rs2 = dbm.getAllPosts(cid, 0, 0);
            if (rs2==null) {
                out.print("<p>NO POSTS TO CHAT</p>");
            }
            else
            {
                out.println("<table border=\"0\" cellpadding=\"6\">");

                while(rs2.next()) {          
                    String user = rs2.getString("uName");
                    Timestamp t = rs2.getTimestamp("mtime");
                    String message = rs2.getString("message");
                    int recipID = rs2.getInt("rID");
                    
                    // Append the chat entry to the response string
                    String dt = tf.format(t);
                    responseString += "<tr><td valign=\"top\">" + dt + "</td>"
                            + "<td valign=\"top\"><b>" + user + "</b>";
                    
                    // If this is a private message, add a string to tell us
                    // who the recipient was
                    if(recipID > 0) {
                        responseString += " <i>to " + h.get("" + recipID) + "</i>";
                    }
                    responseString += "</td><td> " + message + "</td></tr>";
                }
                responseString += ("</table>");
                rs2.close();
            }
            out.print(responseString);

        } catch (SQLException se) {
                se.printStackTrace(out);
        } finally {
                dbm.closeConnection();
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
